package com.wuhan.template.module.home.adapter;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseActivity;
import com.wuhan.template.constant.Urls;
import com.wuhan.template.module.home.activity.MainActivity;
import com.wuhan.template.module.home.entity.SearchDetailEntity;
import com.wuhan.template.utils.EventMessage;
import com.wuhan.template.utils.OkHttpUtils;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/5/14 0014.
 */
public class SearchDetailActivity extends BaseActivity {


    @BindView(R.id.search_webView)
    protected WebView webView;

    @BindView(R.id.feileiName)
    protected TextView title;

    @BindView(R.id.search_backImage)
    protected ImageView backImage;

    private int searchId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_detail;
    }

    @Override
    protected void initViews() {
        searchId=  getIntent().getIntExtra("searchId",0);

        backImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SearchDetailActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    protected void loadData() {

        OkHttpUtils.get(Urls.HOME_SEARCH_DETAIl + "&id=" + searchId, null);

    }

    @Override
    protected void postEventResult(@NonNull EventMessage message) {

        if (message.type.equals(Urls.HOME_SEARCH_DETAIl + "&id=" + searchId)) {
            Gson gson = new Gson();
            final SearchDetailEntity searchDetailEntity = gson.fromJson(message.result, SearchDetailEntity.class);
            title.setText(searchDetailEntity.getColumn_name());
            webView.loadUrl(searchDetailEntity.getContent_url());
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    webView.loadUrl(searchDetailEntity.getContent_url());
                    return true;

                }
            });
        }
    }
}
